home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / extra_2 / flmgmtcl.zip / FILELIST.CPP < prev    next >
C/C++ Source or Header  |  1995-11-02  |  6KB  |  216 lines

  1. // ==========================================================================
  2. //                             Class Implementation : CFileList
  3. // ==========================================================================
  4.  
  5. // Source file : filelist.cpp
  6.  
  7. // Source : Periphere NV (R.Mortelmans)
  8. // Creation Date :        2nd November 1995
  9. // Last Modification : 2nd November 1995
  10.                           
  11. // //////////////////////////////////////////////////////////////////////////
  12.  
  13. #include "stdafx.h"        // standard MFC include
  14. #include "filelist.h"    // class specification
  15. #include "dos.h"        // for _dos_findfirst, ...
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. IMPLEMENT_DYNAMIC(CFileList, CObject)
  23.  
  24. #define new DEBUG_NEW
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Definition of static members
  28.  
  29.  
  30. // Data members -------------------------------------------------------------
  31. // protected:
  32.     // CPath m_path;
  33.     // --- The path specification where to look for files
  34.     
  35.     // CObList m_fileList;            // Contains objects of class CFileSpec
  36.     // --- The list of file specifications found in the above specified path
  37.  
  38. // private:
  39.     
  40. // Member functions ---------------------------------------------------------
  41. // public:
  42.  
  43. CFileList::CFileList()
  44.     :
  45.     m_path()
  46.     {
  47.     }
  48.     
  49. CPathSpec CFileList::GetPath() const
  50.     {
  51.     return m_path;
  52.     }
  53.     
  54. BOOL CFileList::SetPath(CPathSpec path)
  55.     {
  56.     if (path.GetFileName().IsEmpty())
  57.         path.SetFileName(TEXT("*.*"));
  58.     if (path.MakeAbsolute())
  59.         {
  60.         m_path = path;
  61.         return TRUE;
  62.         }
  63.     else
  64.         {
  65.         TRACE(TEXT("CFileList::SetPath : Path spec is invalid : %s\n"), path.GetPath());
  66.         return FALSE;
  67.         }
  68.     }   
  69.     
  70. BOOL CFileList::Search()
  71.     {
  72. #ifdef WIN32
  73.     CFileSpec* pFile;
  74.     WIN32_FIND_DATA fileData;
  75.     HANDLE hFindFile;
  76.     BOOL bFileFound(TRUE);
  77.         
  78.     hFindFile = FindFirstFile(m_path.GetPath(), &fileData);
  79.     if(hFindFile != INVALID_HANDLE_VALUE)
  80.         {
  81.         while (bFileFound)
  82.             {
  83.             if ((fileData.dwFileAttributes & ~FILE_ATTRIBUTE_DIRECTORY) &&
  84.                 (fileData.cFileName[0] != __TEXT('.')) && 
  85.                 (fileData.cFileName[0] != __TEXT('..')))
  86.                 {
  87.                 pFile = new CFileSpec;
  88.                 pFile->SetFileName(fileData.cFileName);
  89.                 pFile->SetTime(CTime(fileData.ftLastWriteTime));
  90.                 ASSERT(fileData.nFileSizeHigh == 0);
  91.                 pFile->SetLength(fileData.nFileSizeLow);
  92.                 pFile->SetAttributes((CFile::Attribute)fileData.dwFileAttributes);
  93.                 m_fileArray.Add(pFile);
  94.                 }
  95.  
  96.             bFileFound = FindNextFile(hFindFile,&fileData);
  97.             }
  98.         FindClose(hFindFile);
  99.         }
  100.     return TRUE;
  101.  
  102. #else
  103.     CFileSpec* pFile;
  104.     _find_t fileInfo;
  105.     BOOL bFileFound;
  106.     
  107.     bFileFound = (_dos_findfirst(m_path.GetPath(), _A_NORMAL | _A_ARCH, &fileInfo) == 0);
  108.     while (bFileFound)
  109.         {
  110.         pFile = new CFileSpec;
  111.         pFile->SetFileName(fileInfo.name);
  112.         pFile->SetTime(CTime((WORD)fileInfo.wr_date, (WORD)fileInfo.wr_time));
  113.         pFile->SetLength(fileInfo.size);
  114.         pFile->SetAttributes((CFile::Attribute)fileInfo.attrib);
  115.         m_fileArray.Add(pFile);
  116.         bFileFound = (_dos_findnext(&fileInfo) == 0);
  117.         }        
  118.     return TRUE;
  119. #endif
  120.     }
  121.     
  122. const CObArray* CFileList::GetList() const
  123.     {
  124.     return &m_fileArray;
  125.     }
  126.  
  127. const CFileSpec* CFileList::GetAt(int nIndex) const
  128. {
  129.     return (CFileSpec*) m_fileArray.GetAt(nIndex);
  130. }
  131.     
  132. void CFileList::Sort()
  133.     {
  134.     // This algorithm will sort a list using selection sort
  135.     // The array contains objects from position 0 to END
  136.     // nIndexNotSorted points to the first not yet sorted object,
  137.     //  so the set [0..nIndexNotSorted - 1] is already sorted
  138.     // This algorithm searches the not yet sorted objects in the set
  139.     //  [nIndexNotSorted..END] for the smallest object and adds this 
  140.     //  to the sorted objects
  141.     // nIndexCompare point to the last object in this set that has 
  142.     //  already been examined
  143.     // The algorithm ends when all objects are sorted
  144.     int nIndexNotSorted = 0;
  145.     int nIndexCompare = 0;
  146.     CFileSpec* pSmallestFile = NULL;    // point to array[nIndexSorted]
  147.     CFileSpec* pCompareFile = NULL;        // point to array[nIndexCompare]
  148.     CFileSpec* pSwapFile;
  149.     
  150.     while (nIndexNotSorted <= m_fileArray.GetUpperBound())
  151.         {
  152.         ASSERT(m_fileArray[nIndexNotSorted]->IsKindOf(RUNTIME_CLASS(CFileSpec)));
  153.         pSmallestFile = (CFileSpec*)m_fileArray[nIndexNotSorted];
  154.         nIndexCompare = nIndexNotSorted;
  155.         nIndexCompare++;
  156.         
  157.         while (nIndexCompare <= m_fileArray.GetUpperBound()) 
  158.             {
  159.             ASSERT(m_fileArray[nIndexCompare]->IsKindOf(RUNTIME_CLASS(CFileSpec)));
  160.             pCompareFile = (CFileSpec*)m_fileArray[nIndexCompare];
  161.             if (*pCompareFile < *pSmallestFile)
  162.                 {
  163.                 pSwapFile = pSmallestFile;
  164.                 m_fileArray[nIndexNotSorted] = m_fileArray[nIndexCompare];
  165.                 m_fileArray[nIndexCompare] = pSwapFile;
  166.                 pSmallestFile = (CFileSpec*)m_fileArray[nIndexNotSorted];
  167.                 }
  168.             nIndexCompare++;
  169.             }
  170.         nIndexNotSorted++;
  171.         }
  172.     }
  173.     
  174. void CFileList::ClearList()
  175.     {
  176.     CFileSpec* pFile;
  177.     int nIndex = 0;
  178.     int nMaxIndex = m_fileArray.GetUpperBound();
  179.     while (nIndex <= nMaxIndex) 
  180.         {
  181.         ASSERT(m_fileArray[nIndex]->IsKindOf(RUNTIME_CLASS(CFileSpec)));
  182.         pFile = (CFileSpec*)m_fileArray[nIndex];
  183.         delete pFile;
  184.         nIndex++;
  185.         }
  186.     m_fileArray.RemoveAll();
  187.     }
  188.     
  189. #ifdef _DEBUG
  190. void CFileList::Dump(CDumpContext& dc) const
  191.     {
  192.     CObject::Dump(dc);
  193.     dc << TEXT("\nm_path : ");
  194.     m_path.Dump(dc);
  195.     dc << TEXT("\nm_fileArray : ") << m_fileArray;    
  196.     }
  197.  
  198. void CFileList::AssertValid() const
  199.     {
  200.     CObject::AssertValid();
  201.     }
  202. #endif
  203.  
  204. CFileList::~CFileList()
  205.     {
  206.     ClearList();
  207.     }
  208.     
  209. // protected:
  210.  
  211. // private:
  212.  
  213. // Message handlers ---------------------------------------------------------
  214.  
  215. // ==========================================================================
  216.